package com.lzb.musicplayer.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.FileUtils;
import com.lzb.basic.utils.MediaUtil;
import com.lzb.basic.utils.SPUtil;
import com.lzb.basic.bean.MediaMetadataInfo;
import com.lzb.musicplayer.BasePlayerActivity;
import com.lzb.musicplayer.Content;
import com.lzb.musicplayer.R;
import com.lzb.musicplayer.bean.MusicFile;
import com.lzb.musicplayer.databinding.ActivityInfoBinding;
import com.lzb.musicplayer.utils.MusicScannerUtil;

import java.util.List;

public class InfoActivity extends BasePlayerActivity<ActivityInfoBinding> {

    private MusicFile musicFile;
    private boolean isFirst;

    @Override
    protected int onCreate() {
        return R.layout.activity_info;
    }

    @Override
    protected void initView() {
        mDataBinding.ivInfoBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());


        Intent intent = getIntent();
        List<MusicFile> list = (List<MusicFile>) intent.getSerializableExtra("list");

        if (list != null && !list.isEmpty()) {
            mDataBinding.audioPlayerView.setPlayList(list, 0);
            MusicFile musicFile = list.get(0);
            setFile(musicFile);
        }
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    protected void initData() {
        getMusicInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void setFile(MusicFile musicFile) {
        this.musicFile = musicFile;
        if (!TextUtils.isEmpty(musicFile.getTitle())) {
            mDataBinding.tvName.setText(musicFile.getTitle());
        }
        if (!TextUtils.isEmpty(musicFile.getArtist())) {
            mDataBinding.tvArtist.setText(musicFile.getArtist());
        }

        Bitmap albumCover = MusicScannerUtil.getAlbumArt(this, musicFile.getAlbumCover());
        Bitmap bit = MusicScannerUtil.getAlbumArt((Activity) this, musicFile.getArtistCover());
        mDataBinding.ivImg.setImageResource(R.drawable.pic);
        if (albumCover != null) {
            mDataBinding.ivImg.setImageBitmap(albumCover);
        } else {
            if (bit != null) {
                mDataBinding.ivImg.setImageBitmap(bit);
            }
        }

        // 设置完音乐文件后，更新音频信息
        initData();
    }

    private void getMusicInfo() {
        boolean isOpen = SPUtil.getBoolean(mContext, Content.isOpenMusicInfo, false);
        if (isOpen && musicFile != null) {
            String format = FileUtils.getFileExtension(musicFile.getPath());

            // 获取音频文件的详细信息
            MediaMetadataInfo metadataInfo = MediaUtil.getMediaMetadataInfo(musicFile.getPath());
            if (metadataInfo != null) {
                // 格式化比特率信息
                String bitrateInfo = "";
                if (metadataInfo.getBitrate() > 0) {
                    int bitrateKbps = metadataInfo.getBitrate() / 1000; // 转换为kbps
                    bitrateInfo = bitrateKbps + "kbps";
                }

                // 格式化采样率信息
                String sampleRateInfo = "";
                if (metadataInfo.getSampleRate() > 0) {
                    sampleRateInfo = metadataInfo.getSampleRate() / 1000 + "kHz";
                }

                // 组合显示信息
                StringBuilder infoBuilder = new StringBuilder();
                if (!TextUtils.isEmpty(format)) {
                    infoBuilder.append(format);
                }
                if (!TextUtils.isEmpty(bitrateInfo)) {
                    if (infoBuilder.length() > 0) {
                        infoBuilder.append(" • ");
                    }
                    infoBuilder.append(bitrateInfo);
                }
                if (!TextUtils.isEmpty(sampleRateInfo)) {
                    if (infoBuilder.length() > 0) {
                        infoBuilder.append(" • ");
                    }
                    infoBuilder.append(sampleRateInfo);
                }

                // 设置到TextView显示
                String displayInfo = infoBuilder.toString();
                if (!TextUtils.isEmpty(displayInfo)) {
                    mDataBinding.tvMusicInfo.setText(displayInfo);
                    mDataBinding.tvMusicInfo.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.tvMusicInfo.setVisibility(View.GONE);
                }
            } else {
                // 如果获取失败，只显示格式信息
                if (!TextUtils.isEmpty(format)) {
                    mDataBinding.tvMusicInfo.setText(format.toUpperCase());
                    mDataBinding.tvMusicInfo.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.tvMusicInfo.setVisibility(View.GONE);
                }
            }
        } else {
            // 如果设置未开启或没有音乐文件，隐藏信息
            mDataBinding.tvMusicInfo.setVisibility(View.GONE);
        }
    }


    @Override
    public void onPlaybackStateChanged(boolean isPlaying) {

    }

    @Override
    public void onPlaybackStateChanged(int state) {

    }

    @Override
    public void onProgressUpdated(long position, long duration) {

    }

    @Override
    public void onTrackChanged(MusicFile currentTrack) {
        setFile(currentTrack);
        getMusicInfo();
    }
}